<template>
	<view class="gui-margin">
		<view class="gui-margin-top">
			<text class="gui-h5 gui-color-gray gui-bold">选择商家</text>
		</view>
		<view style="margin-top:15rpx;min-height: 300rpx;">
			<gui-select-list-custom @change="change1" :items="lists"></gui-select-list-custom>
		</view>

		<!-- 底部按钮栏 -->
		<view class="product-footer gui-bg-white gui-border-t">
			<view class="gui-flex gui-rows gui-space-between gui-align-items-center gui-border-t">
				<!-- 2个底部按钮 -->
				<view class="gui-footer-icon-buttons" style="margin-left:30rpx;" hover-class="gui-tap" @tap="goHome">
					<text
						class="gui-footer-icon-buttons-icon gui-block-text gui-icons gui-primary-color">&#xe63b;</text>
					<text class="gui-footer-icon-buttons-text gui-block-text gui-icons gui-primary-color">首页</text>
				</view>
				<view class="gui-footer-icon-buttons" hover-class="gui-tap">
					<button type="default" plain="true" open-type="contact" style="border: none;width: 100rpx;"><text
							class="gui-footer-icon-buttons-icon gui-block-text gui-icons gui-primary-color">&#xe626;</text>
						<text
							class="gui-footer-icon-buttons-text gui-block-text gui-icons gui-primary-color">客服</text></button>

				</view>
				<!-- 2个大按钮 -->
				<view class="gui-flex1 gui-footer-large-buttons gui-flex gui-rows gui-nowrap gui-justify-content-end">
					<!-- <view class=" gui-footer-large-button gui-text-center gui-bg-add-card" @tap="addtocard"
						style="border-bottom-left-radius:50rpx; border-top-left-radius:50rpx;" hover-class="gui-tap">
						<text
							class="gui-text gui-color-white gui-footer-large-button-text gui-block-text gui-text-center">加入购物车</text>
					</view> -->
					<view class="gui-footer-large-button gui-text-center gui-bg-buy" @tap="buynow"
						style="border-radius:50rpx;" hover-class="gui-tap">
						<text
							class="gui-text gui-color-white gui-footer-large-button-text gui-block-text gui-text-center">立即购买</text>
					</view>
				</view>
			</view>

			<gui-iphone-bottom></gui-iphone-bottom>
		</view>



	</view>
</template>
<script>
	export default {
		data() {
			return {
				lists: [],
				teacher_id: 0,
				day: '',
				time: '',
				money: 0,
				shop_id: 0,
				week:0
			}
		},
		onLoad: function(query) {
			this.$globalMethod();
			if (typeof query.teacher_id != 'undefined' && typeof query.day != 'undefined' && typeof query.time !=
				'undefined' && typeof query.money != 'undefined' && typeof query.week !='undefined') {
				this.teacher_id = query.teacher_id;
				this.day = query.day;
				this.time = query.time;
				this.money = query.money;
				this.week = query.week;
				this.shop_list();
			}
		},
		methods: {
			goHome() {
				uni.reLaunch({
					url: '/pages/index/index'
				})
			},
			buynow() {
				let _this = this;
					
				_this.graceJS.post(_this.serverUrl + '/api.shop/buy', {
					teacher_id: _this.teacher_id,
					shop_id: _this.shop_id,
					appointment_day: _this.day,
					st: _this.time,
					et: _this.time,
					money:_this.money,
					week:_this.week
				}, 'json', {
					token: _this.token
				}, (res) => {
					if(res.code==0){
						var data = res.data;
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: data.timestamp,
							nonceStr: data.nonce_str,
							package: 'prepay_id='+data.prepay_id,
							signType: 'MD5',
							paySign: data.pay_sign,
							success: function(res) {
								
								
								
								uni.requestSubscribeMessage({
									tmplIds:['hKsm9YexxoBuiiK64iBV2g_rKuLp9Z2lrPZLWJl8fCc','RXqvOQxLlMH4deSs_X66UumvyPozE0j7aVcLQArBfDU','VuZBskoDR0Mwr_9yQvAeoVVK1gHjwaVLrnUe_N0hdv8'],
									success(res){
										// console.log(res);
									}
								});	
								
								uni.reLaunch({
									url:'/pages/order/list',
									success:function(){
										uni.showToast({
											title:'支付成功',
											icon:'none'
										})
									}
								})
								
								
								
							},
							fail: function(err) {
								if(err.errMsg=='requestPayment:fail cancel'){
									uni.showToast({
										title: '用户取消支付',
										icon: 'none'
									});
								}else{
									uni.showToast({
										title: '用户支付失败',
										icon: 'none'
									});
								}
								
								return false;
							}
						});
					}else{
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				}, (e) => {
					uni.showToast({
						title:'网络请求失败，请稍后再试',
						icon:'none'
					})
				})
			},
			shop_list() {
				let _this = this;
				_this.graceJS.get(_this.serverUrl + '/api.shop/get_teacher_shop_list', {
					teacher_id: _this.teacher_id
				}, {
					token: _this.token
				}, (res) => {
					if (res.code == 0) {
						_this.lists = res.data.list;
						_this.shop_id = _this.lists[0].id;
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}, (e) => {
					uni.showToast({
						title: '网络请求错误，请稍后再试',
						icon: 'none'
					})
				})
			},

			change1: function(e) {
				this.shop_id = this.lists[e].id;
			},
		}
	}
</script>
<style>
	.demo {
		width: 210rpx;
		margin: 10rpx;
	}

	.button {
		width: 90rpx;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 8rpx;
		margin: 10rpx;
	}

	.button-text {
		font-size: 38rpx;
		text-align: center;
	}

	.product-name {
		width: 560rpx;
		line-height: 50rpx;
	}

	.product-share {
		width: 80rpx;
		height: 80rpx;
		text-align: center;
		font-size: 50rpx;
		color: #FF7900;
		line-height: 80rpx;
	}

	.product-price {
		color: #FF7900;
		line-height: 60rpx;
		font-size: 30rpx;
		font-weight: bold;
	}

	.gui-common-line {
		height: 18rpx;
	}

	.product-footer {
		position: fixed;
		z-index: 99;
		left: 0;
		bottom: 0;
		width: 750rpx;
	}

	.product-attr {
		width: 700rpx;
		margin: 25rpx;
		height: 580rpx;
	}
</style>